`
https://leetcode.cn/problems/longest-repeating-character-replacement/
`

/**
 * @param {string} s
 * @param {number} k
 * @return {number}
 */
var characterReplacement = function (s, k) {
  let left = 0, right = 0

  // 记录窗口内每个大写字母出现的次数
  const record = new Array(26).fill(0)
  const asciiOfA = 'A'.charCodeAt(0)

  // 保存窗口内出现次数最多的字符
  let maxCount = 0

  let res = 0

  while (right < s.length) {
    // 获取 ascii
    const c = s.charCodeAt(right) - asciiOfA
    record[c]++
    maxCount = Math.max(maxCount, record[c])
    right++

    // 当替换次数不够时，即当前窗口内无法将所有字符替换为出现次数最多的字符时
    // 需要缩小窗口，即窗口大小减去重复次数最多的字符数依然大于可以替换的次数
    while (right - left - maxCount > k) {
      const d = s.charCodeAt(left) - asciiOfA
      record[d]--
      maxCount = Math.max(maxCount, record[d])
      left++
    }

    // 此时窗口一定是符合条件的，更新长度
    res = Math.max(res, right - left)
  }

  return res
};